import { getStorage, setStorage } from "@/utils/index"

const state = {
  token: getStorage("token") || "",
  userInfo: getStorage("userInfo") || null
};

const getters = {
  token() {
    return state.token;
  },
  userInfo(state) {
    return state.userInfo;
  }
};

const mutations = {
  async setToken(state, data) {
    state.token = data;
    await setStorage("token", data);
  },
  async setUserInfo(state, data) {
    state.userInfo = data;
    await setStorage("userInfo", data);
  }
};

const actions = {
  setToken({ commit }, data) {
    commit("setToken", data);
  },
  setUserInfo({ commit }, data) {
    commit("setToken", data.token);
    commit("setUserInfo", data);
  }
};

export default {
  state,
  mutations,
  actions,
  getters,
};
